package singlelinkedlist;

import java.util.Scanner;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * 用于测试链表
 */
@SuppressWarnings({"all"})
public class Main {

    public static void main(String[] args) {
        SingleLinkedList list = new SingleLinkedList();
        Scanner in = new Scanner(System.in);
        while (true){
            switch (menu()){
                case 0:
                    System.exit(0);
                case 1:
                    System.out.println("请输入你要添加的节点个数: ");
                    int n = in.nextInt();
                    while (n-- != 0){
                        list.addFirst(in.nextInt());
                    }
                    list.display();
                    break;
                case 2:
                    System.out.println("请输入你要添加的节点个数: ");
                    n = in.nextInt();
                    while (n-- != 0){
                        list.addLast(in.nextInt());
                    }
                    list.display();
                    break;
                case 3:
                    System.out.println("当前链表的长度为: " + list.size());
                    break;
                case 4:
                    System.out.print("请输入期望插入的 index 和 data 以空格分隔: ");
                    int index = in.nextInt();
                    int data = in.nextInt();
                    if(list.addIndex(index, data)){
                        list.display();
                    }
                    break;
                case 5:
                    list.clear();
                    System.out.println("清除成功!");
                    list.display();
                    break;
                case 6:
                    System.out.print("请输入要查找的key: ");
                    int key = in.nextInt();
                    if (list.contains(key)){
                        System.out.println("存在！");
                    }else {
                        System.out.println("不存在！");
                    }
                    break;
                case 7:
                    System.out.print("请输入要删除的key: ");
                    key = in.nextInt();
                    list.remove(key);
                    list.display();
                    break;
                case 8:
                    System.out.print("请输入要删除的key: ");
                    key = in.nextInt();
                    list.removeAllKey(key);
                    list.display();
                    break;

            }
        }
    }

    // 打印菜单
    // 返回选择
    public static int menu(){
        System.out.println("\n===================SingleLinkedList测试菜单==================");
        System.out.println("1.使用头插法添加元素");
        System.out.println("2.使用尾插法添加元素");
        System.out.println("3.获取当前链表的长度");
        System.out.println("4.在指定索引index处插入元素");
        System.out.println("5.清空链表");
        System.out.println("6.查找key是否存在");
        System.out.println("7.删除链表第一个key");
        System.out.println("8.删除链表所有key");
        System.out.println("0.退出系统");
        System.out.print("请输入你的选择: ");
        int choice = new Scanner(System.in).nextInt();
        return choice;
    }
}
